<%@ page pageEncoding="UTF-8" %>

<table style="width: 360px;">

</table>
<br /><br />

<!--<a href="/sync/start">禁用定时任务</a><br />
<a href="/sync/stop">恢复定时任务</a><br />-->

<script>
    // 展示任务列表
    showTasks();
    // 定时刷新任务列表
    setInterval(function() {showTasks();}, 5000);
    $("#switchTask").click(function() {switchTask();});
    $("#exec").click(function() {exec();});

    /** 展示所有任务 */
    function showTasks() {
        const result = ajaxMethod("showTasks");
        let lists = "<tr><th colspan=\"4\">任务列表</th><\/tr>";

        if (result.code == 0) {
            $.each(result.data, function(index, item) {
                // 实际数据处理应该由后端去执行
                let ops = "", status = "";
                if (item.open == true) {
                    ops = "关闭 定时任务";
                    if (item.available == true) {
                        status = "<span style='color: green'>正常<\/span>";
                    } else {
                        status = "<span style='color: #ff0000'>不可用<\/span>";
                    }
                } else {
                    ops = "开启 定时任务";
                }

                lists += "<tr>";
                lists += "    <td>";
                lists += "        【"+item.name+"】";
                lists += "    <\/td>";
                lists += "    <td>";
                lists += "        <a href='#' onclick=\"switchTask(\'"+item.taskId+"\')\">"+ops+"<\/a>";
                lists += "    <\/td>";
                lists += "    <td>";
                lists += "        <a href='#' onclick=\"exec(\'"+item.taskId+"\')\">手动执行<\/a>";
                lists += "    <\/td>";
                lists += "    <td>";
                lists += "        "+status;
                lists += "    <\/td>";
                lists += "<tr>";
                lists += "<\/tr>";

            });
        //==>end if
        } else {
            alert("加载失败");
        }

        // 渲染表格
        $("table").html(lists);
    }

    /** 开/关定时任务 */
    function switchTask(taskId) {
        const result = ajaxMethod("switchTask", {taskId: taskId});
        if (result.code == 0) {
            showTasks();
        } else {
            alert("操作失败");
        }
    }

    /** 手动执行一次任务 */
    function exec(taskId) {
        const result = ajaxMethod("exec", {taskId: taskId});
        showTasks();
        if (result.code == 0) {
            alert("操作成功");
        } else {
            alert(result.message);
        }
    }

</script>
